package com.example.sqlite.dao;

import java.util.ArrayList;
import java.util.List;

import com.example.sqlite.models.Contact;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ContactsDAO {

	private SQLiteDatabase sqLiteDatabase;
	private final String[] columns = { CustomSQLiteOpenHelper.COLUMN_ID,
			CustomSQLiteOpenHelper.COLUMN_NAME,
			CustomSQLiteOpenHelper.COLUMN_FONE };
	private final CustomSQLiteOpenHelper customSQLiteOpenHelper;

	public ContactsDAO(Context context) {
		customSQLiteOpenHelper = new CustomSQLiteOpenHelper(context);
	}

	public void open() throws SQLException {
		sqLiteDatabase = customSQLiteOpenHelper.getWritableDatabase();
	}

	public void close() {
		customSQLiteOpenHelper.close();
	}

	public Contact create(String name, String fone) {
		ContentValues values = new ContentValues();
		values.put(CustomSQLiteOpenHelper.COLUMN_NAME, name);
		values.put(CustomSQLiteOpenHelper.COLUMN_FONE, fone);

		long insertId = sqLiteDatabase.insert(
				CustomSQLiteOpenHelper.TABLES_CONTACTS, null, values);

		Cursor cursor = sqLiteDatabase.query(
				CustomSQLiteOpenHelper.TABLES_CONTACTS, columns,
				CustomSQLiteOpenHelper.COLUMN_ID + " = " + insertId, null,
				null, null, null);

		cursor.moveToFirst();
		Contact contact = new Contact();
		contact.setId(cursor.getLong(0));
		contact.setName(cursor.getString(1));
		contact.setFone(cursor.getString(2));
		cursor.close();
		return contact;
	}

	public void delete(Contact contact) {
		long id = contact.getId();

		sqLiteDatabase.delete(CustomSQLiteOpenHelper.TABLES_CONTACTS,
				CustomSQLiteOpenHelper.COLUMN_ID + " = " + id, null);
	}

	public List<Contact> getAll() {
		List<Contact> contacts = new ArrayList<Contact>();

		Cursor cursor = sqLiteDatabase.query(
				CustomSQLiteOpenHelper.TABLES_CONTACTS, columns, null, null,
				null, null, null);

		cursor.moveToFirst();

		while (!cursor.isAfterLast()) {
			Contact contact = new Contact();
			contact.setId(cursor.getLong(0));
			contact.setName(cursor.getString(1));
			contact.setFone(cursor.getString(2));
			contacts.add(contact);
			cursor.moveToNext();
		}
		cursor.close();
		return contacts;
	}

	public Contact get(long id) {
		Contact contact = new Contact();
		Cursor cursor = sqLiteDatabase.query(
				CustomSQLiteOpenHelper.TABLES_CONTACTS, columns,
				CustomSQLiteOpenHelper.COLUMN_ID + " = " + id, null, null,
				null, null);
		cursor.moveToFirst();
		contact.setId(cursor.getLong(0));
		contact.setName(cursor.getString(1));
		contact.setFone(cursor.getString(2));
		cursor.close();

		return contact;
	}

	public void edit(long id, String name, String fone) {
		ContentValues values = new ContentValues();
		values.put(CustomSQLiteOpenHelper.COLUMN_NAME, name);
		values.put(CustomSQLiteOpenHelper.COLUMN_FONE, fone);
		sqLiteDatabase.update(CustomSQLiteOpenHelper.TABLES_CONTACTS, values,
				CustomSQLiteOpenHelper.COLUMN_ID + " = " + id, null);
	}

}
